import cutFile from '@/utils/cutFile';
import { parallelTask } from '@/utils/parallelTask';
import { useState } from 'react';

export default function BigUpload() {
  const [chunks, setChunks] = useState([]);
  const [startDate, setStartDate] = useState<number>();
  const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
    const time = Date.now();
    const chunks = (await cutFile(e.target.files[0])) as any[];
    const times = Date.now() - time;
    setStartDate(times);
    setChunks(chunks);
    upload();
  };
  const asyncFuns = () => {
    return chunks.map((item) => {
      return async () => {
        await new Promise((resolve) => {
          return item;
        });
      };
    });
  };
  const upload = () => {
    const asyncFs = asyncFuns();
    // 并发上传
    parallelTask(asyncFs, 4).then((e) => {
      console.log('成功');
    });
  };
  return (
    <div>
      <input type="file" onChange={handleChange} />
      {chunks.length !== 0 ? (
        <div>
          <div>时间：{startDate / 1000}s</div>
          <div>{JSON.stringify(chunks)}</div>
        </div>
      ) : null}
    </div>
  );
}
